package com.sptpc.drink.controller;

import com.sptpc.drink.pojo.Category;
import com.sptpc.drink.pojo.Drink;
import com.sptpc.drink.service.DrinkService;
import com.sptpc.drink.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/drink")
public class DrinkController {

    @Autowired
    private DrinkService drinkService;

    @GetMapping(value = "/")
    public ServerResponse queryAll() {
        return ServerResponse.createBySuccess(drinkService.queryAll());
    }

    @GetMapping(value = "/{id}")
    public ServerResponse queryById(@PathVariable("id") long id) {
        return ServerResponse.createBySuccess("成功", drinkService.queryById(id));
    }

    @GetMapping(value = "/", params = "name")
    public ServerResponse queryByName(@RequestParam String name) {
        return ServerResponse.createBySuccess("成功", drinkService.queryByName(name));
    }

    @PostMapping(value = "/save", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ServerResponse insert(@RequestBody Drink drink) {
        drink.setCreateTime(new Date());
        drink.setUpdateTime(new Date());
        drinkService.insert(drink);
        return ServerResponse.createBySuccessMessage("插入成功");
    }

    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ServerResponse update(@RequestBody Drink drink) {
        drink.setUpdateTime(new Date());
        drinkService.update(drink);
        return ServerResponse.createBySuccessMessage("更新成功");
    }

    @GetMapping(value = "/delete/{id}")
    public ServerResponse delete(@PathVariable("id") long id) {
        drinkService.delete(id);
        return ServerResponse.createBySuccessMessage("删除成功");
    }

}
